﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace MvcControlToolkit
{
    /// <summary>
    /// The DragPanel extender allows users to easily add "draggability" to their 
    /// controls. The DragPanel targets any ASP.NET Panel and takes an additional 
    /// parameter that signifies the control to use as the "drag handle". 
    /// Once initialized, the user can freely drag the panel around the web page 
    /// using the drag handle
    /// </summary>
    public static class DragPanelExtensions
    {
        /// <summary>
        /// Creates a DragPanel extender with the specified handle element
        /// </summary>
        /// <param name="elementId">The ID of a Panel to make draggable</param>
        /// <param name="handleElementId">The ID of a control that will serve as the "drag handle" for the panel. When the user clicks and drags this control, the panel will move</param>
        /// <returns></returns>
        public static string DragPanel(this AjaxHelper helper, string elementId, string handleElementId)
        {
            //return a drag panel
            return helper.DragPanel(elementId, new { handle = new ElementReference(handleElementId) });
        }

        /// <summary>
        /// Creates a DragPanel extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of a Panel to make draggable</param>
        /// <param name="properties">An object containing the properties to set</param>
        public static string DragPanel(this AjaxHelper helper, string elementId, object properties)
        {
            var sb = new StringBuilder();

            // Add Microsoft Ajax library   
            sb.Append(helper.MicrosoftAjaxInclude());

            // Add toolkit scripts   
            sb.Append(helper.ToolkitInclude(
                    "AjaxControlToolkit.ExtenderBase.BaseScripts.js",
                    "AjaxControlToolkit.Common.Common.js",
                    "AjaxControlToolkit.Compat.Timer.Timer.js",
                    "AjaxControlToolkit.Compat.DragDrop.DragDropScripts.js",
                    "AjaxControlToolkit.DragPanel.FloatingBehavior.js"
                ));

            // Perform $create               
            sb.AppendLine(helper.Create("AjaxControlToolkit.FloatingBehavior", properties, elementId));

            return sb.ToString();
        }
    }
}
